import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../constant/color_constant.dart';

class EcyTextFieldWidget extends StatefulWidget {
  final String inputValue;
  final String hint;
  int maxLimit = 11;
  final ValueChanged<String> updateMessageCallback2;
  EcyTextFieldWidget(
      {required this.hint,
      required this.inputValue,
      required this.maxLimit,
      required this.updateMessageCallback2});

  @override
  _EcyTextFieldWidgetState createState() => _EcyTextFieldWidgetState(
      hint: hint,
      inputValue: inputValue,
      maxLimit: maxLimit,
      updateMessageCallback: updateMessageCallback2);
}

class _EcyTextFieldWidgetState extends State<EcyTextFieldWidget> {
  final TextEditingController _controller = TextEditingController();
  final String inputValue;
  final String hint;
  final int maxLimit;
  ValueChanged<String> updateMessageCallback;
  _EcyTextFieldWidgetState(
      {required this.hint,
      required this.inputValue,
      required this.maxLimit,
      required this.updateMessageCallback});
  @override
  Widget build(BuildContext context) {
    return TextField(
        inputFormatters: [
          FilteringTextInputFormatter.digitsOnly,
          LengthLimitingTextInputFormatter(maxLimit)
        ],
        decoration: InputDecoration(
            isDense: true,
            border: UnderlineInputBorder(
              borderRadius: BorderRadius.circular(8.r),
              borderSide: BorderSide(
                color: Color(0x0A000000), // 修改下划线颜色
                width: 0, // 修改下划线宽度
              ),
            ), // 移除默认边框
            focusedBorder: UnderlineInputBorder(
              // 获取焦点时的下划线
              borderSide: BorderSide(
                color: Color(0x0A000000), // 修改下划线颜色
                width: 0, // 修改下划线宽度
              ),
            ),
            enabledBorder: UnderlineInputBorder(
              // 默认状态下的下划线
              borderSide: BorderSide(
                color: Color(0x0A000000), // 修改下划线颜色
                width: .0, // 修改下划线宽度
              ),
            ),
            hintText: hint,
            hintStyle: TextStyle(
                fontFamily: "PingFag SC",
                fontWeight: FontWeight.w500,
                fontSize: 18.sp,
                color: ColorConstant.CommonColorGray)),
        textAlign: TextAlign.start,
        style: TextStyle(
            fontFamily: "PingFag SC",
            fontWeight: FontWeight.w500,
            fontSize: 18.sp,
            color: ColorConstant.CommonColorBlack),
        onEditingComplete: () {
          // 当用户点击完成按钮时，这个函数会被调用;
          // 你可以在这里添加你的逻辑，比如验证输入或提交数据
        },
        onTap: () {
          updateMessageCallback("");
        },
        onChanged: (value) {
          // 这个回调函数会在文本变化时不断被调用
          print('文本变化: $value');
          // updateMessageCallback(value);
        });
  }

  @override
  void dispose() {
    // 清理控制器，避免内存泄漏
    _controller.dispose();
    super.dispose();
  }
}
